#region Copyright note and modification history
/*
-----------------------------------------------------------------------------
Copyright (c) 2006 HSI Inc. All Rights Reserved.

PROPRIETARY NOTICE: This software has been provided pursuant to a
License Agreement that contains restrictions on its use. This software
contains valuable trade secrets and proprietary information of
HSI Inc and is protected by Federal copyright law. It may not
be copied or distributed in any form or medium, disclosed to third parties,
or used in any manner that is not provided for in said License Agreement,
except with the prior written authorization of HSI Inc.

-----------------------------------------------------------------------------
$Log: $
-----------------------------------------------------------------------------
*/
#endregion

using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

namespace SimplePushpinGenerator
{
    /// <summary>
    /// Summary description for ImageHelper.
    /// </summary>
    public static class ImageHelper
    {
        public static byte[] ToByteArray(this Image image)
        {
            if (image == null) return null;

            var stream = new MemoryStream();
            image.Save(stream, image.RawFormat);
            return stream.ToArray();
        }

        public static Image ToImage(this byte[] bytes)
        {
            if (bytes == null) return null;

            var stream = new MemoryStream(bytes);
            return Image.FromStream(stream);
        }

        //Rotate code from
        //http://www.vcskicks.com/image-rotate.php
        public static Image Rotate(this Image image,double angle)
        {
            PointF[] rotationPoints = { new PointF(0, 0),
                                        new PointF(image.Width, 0),
                                        new PointF(0, image.Height),
                                        new PointF(image.Width, image.Height)};

            //Rotate the corners
            PointMath.RotatePoints(rotationPoints, new PointF(image.Width / 2.0f, image.Height / 2.0f), angle);

            //Get the new bounds given from the rotation of the corners
            //(avoid clipping of the image)
            Rectangle bounds = PointMath.GetBounds(rotationPoints);

            //An empy bitmap to draw the rotated image
            var rotatedBitmap = new Bitmap(bounds.Width, bounds.Height);

            using (Graphics g = Graphics.FromImage(rotatedBitmap))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //Transformation matrix
                var m = new Matrix();
                m.RotateAt((float)angle, PointMath.Center(image.Width, image.Height));
                m.Translate(-bounds.Left, -bounds.Top, MatrixOrder.Append); //shift to compensate for the rotation

                g.Transform = m;
                g.DrawImage(image, 0, 0, image.Width, image.Height);
            }
            return rotatedBitmap;
        }
    }
}